import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/card_info_entity.dart';
import 'package:nyh_motorcade_app/pages/driver_manage/driver_details/card_account_info/card_account_info_controller.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CardAccountInfoPage extends StatelessWidget {
  final String driverId;
  final List<CardInfoItem> cardList;
  final ValueChanged<bool>? onUpdateEnableEdit;

  CardAccountInfoPage(this.driverId, this.cardList, this.onUpdateEnableEdit);

  @override
  Widget build(BuildContext context) {
    String tag = DateTime.now().millisecondsSinceEpoch.toString();
    final controller = Get.put(CardAccountInfoController(), tag: tag);
    controller.initData(driverId, cardList, onUpdateEnableEdit);
    return Stack(
      alignment: AlignmentDirectional.center,
      fit: StackFit.expand,
      children: [
        GetBuilder<CardAccountInfoController>(
          tag: tag,
          builder: (controller) {
            return SmartRefresher(
              controller: controller.refreshController,
              enablePullDown: true,
              onRefresh: controller.onRefresh,
              child: MultiStateWidget(
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: BouncingScrollPhysics(),
                  itemBuilder: (context, index) {
                    return buildItemWidget(controller, index);
                  },
                  itemCount: controller.cardList.length,
                ),
                state: controller.multiState,
                onRetry: () {
                  controller.onRetry();
                },
                model: MultiModel.tint,
              ),
            );
          },
        ),
        Positioned(
          right: 16.w,
          bottom: 16.w,
          child: GestureDetector(
            onTap: () {
              controller.goToBindCardPage();
            },
            child: Image.asset("images/ic_bind_circle.png"),
          ),
        ),
      ],
    );
  }

  Widget buildItemWidget(CardAccountInfoController controller, int index) {
    if (controller.cardList.isEmpty) {
      return Container();
    }
    CardInfoItem entity = controller.cardList[index];
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              controller.goToCardDetails(entity.id);
            },
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.only(left: 16.w, top: 10.w, right: 16.w, bottom: 10.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Expanded(
                            child: Text(
                              entity.name ?? "",
                              style: TextStyle(
                                color: Colours.color_text_theme,
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          Visibility(
                            child: Container(
                              margin: EdgeInsets.only(left: 8.w),
                              padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                              decoration: BoxDecoration(
                                color: (entity.cardType ?? "") == Constant.OWN_CARD ? Colours.color_34c759 : Colours.color_theme,
                                borderRadius: BorderRadius.circular(5.w),
                                border: Border.all(
                                  width: 1.w,
                                  color: (entity.cardType ?? "") == Constant.OWN_CARD ? Colours.color_34c759 : Colours.color_theme,
                                ),
                              ),
                              child: Text(
                                entity.cardTypeName ?? "",
                                style: TextStyle(
                                  color: Colours.color_white,
                                  fontSize: 10.sp,
                                ),
                              ),
                            ),
                          ),
                          Visibility(
                            child: Container(
                              margin: EdgeInsets.only(left: 8.w),
                              padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                              decoration: BoxDecoration(
                                color: Colours.color_white,
                                borderRadius: BorderRadius.circular(5.w),
                                border: Border.all(width: 1.w, color: Colours.color_cfcfd5),
                              ),
                              child: Text(
                                entity.energyTypeName ?? "",
                                style: TextStyle(
                                  color: Colours.color_cfcfd5,
                                  fontSize: 10.sp,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5.w,
                      ),
                      Text(
                        entity.cardNumber ?? "",
                        style: TextStyle(
                          color: Colours.color_cfcfd5,
                          fontSize: 14.sp,
                        ),
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Expanded(
                            child: Text(
                              "余额(元)：${FormatUtils.formatMoney(entity.availableAmount)}",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 12.sp,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          TextButtonWithIcon(
                            onPressed: () {
                              showUnboundDialog(controller, entity.id);
                            },
                            icon: Image.asset("images/ic_unbound.png"),
                            label: Text(
                              "解绑",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 14.sp,
                              ),
                            ),
                            textIconPadding: 3.w,
                            style: ButtonStyle(
                              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              minimumSize: MaterialStateProperty.all(Size(0, 0)),
                              padding: MaterialStateProperty.all(EdgeInsets.all(5.w)),
                            ),
                          ),
                          SizedBox(
                            width: 11.w,
                          ),
                          TextButton(
                            onPressed: () {
                              controller.goToRapidTransferPage(entity.id, entity.name, entity.availableAmount);
                            },
                            child: Text(
                              "划拨",
                              style: TextStyle(
                                color: Colours.color_theme,
                                fontSize: 14.sp,
                              ),
                            ),
                            style: ButtonStyle(
                              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              minimumSize: MaterialStateProperty.all(Size(80.w, 30.w)),
                              padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                              side: MaterialStateProperty.all(BorderSide(width: 1.w, color: Colours.color_theme)),
                              shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.w))),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                Visibility(
                  visible: (entity.remark ?? "").isNotEmpty,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Divider(
                        color: Colours.color_divider,
                        height: 1.w,
                        thickness: 1.w,
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 10.w,
                          horizontal: 16.w,
                        ),
                        child: Text(
                          entity.remark ?? "",
                          style: TextStyle(
                            color: Colours.color_cfcfd5,
                            fontSize: 12.sp,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void showUnboundDialog(CardAccountInfoController controller, String? id) {
    showCupertinoDialog(
      barrierDismissible: true,
      context: Get.context!,
      builder: (c) {
        return CupertinoAlertDialog(
          content: Text(
            "是否解绑该卡?",
            style: TextStyle(
              color: Colours.color_text_theme,
              fontSize: 14.sp,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: Text(
                "取消",
                style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
              ),
              onPressed: () {
                //Navigator.pop(c);
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: Text(
                "确定",
                style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
              ),
              onPressed: () {
                //Navigator.pop(c);
                Get.back();
                controller.unboundCard(id);
              },
            ),
          ],
        );
      },
    );
  }
}
